package org.xu.controller;

import com.core.base.PagedReply;
import com.core.base.Reply;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xu.dto.CarInfoDTO;
import org.xu.dto.CarInfoPageDTO;
import org.xu.dto.FileDTO;
import org.xu.entity.AdapterCarInfoEntity;
import org.xu.request.CarPageRequest;
import org.xu.request.SubOrderInfo;
import org.xu.service.AdapterCarInfoService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author liuxiaoxu
 */
@Slf4j
@RequestMapping("/")
@RestController
public class CarController {

    @Autowired
    private AdapterCarInfoService carInfoService;

    @RequestMapping("listCarInfo")
    public Reply<List<CarInfoDTO>> listCarInfo(){
        List<AdapterCarInfoEntity> list = carInfoService.listCarInfo();
        ArrayList<CarInfoDTO> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CarInfoDTO dto = new CarInfoDTO();
            BeanUtils.copyProperties(list.get(i), dto);
            dto.setNumId(new Integer(i));
            result.add(dto);
            dto.setPrice(new BigDecimal(new Random().nextInt(10000)));
            String fileName = i % 4 + ".jpg";
            dto.setPicUrl("http://localhost:9090/"+ fileName);
        }
        return Reply.success(result);
    }

    /**
     * 下单
     */
    @RequestMapping("placeOrder")
    public Reply<String> placeOrder(@RequestBody List<SubOrderInfo> subOrderInfos){
        log.info("下单的数据为:{}",subOrderInfos);
        return Reply.success("下单完成");
    }

    /**
     * 测试分页查询
     */
    @RequestMapping("/car/pageQuery")
    @ResponseBody
    public PagedReply<CarInfoPageDTO> pageQuery(@RequestBody CarPageRequest request){
        log.info("request===:{}",request);
        PageInfo<AdapterCarInfoEntity> pageInfoList = carInfoService.pageQuery(request);
        List<AdapterCarInfoEntity> list = pageInfoList.getList();
        List<CarInfoPageDTO> list2 = list.stream().map(t -> {
            CarInfoPageDTO dto = new CarInfoPageDTO();
            BeanUtils.copyProperties(t, dto);
            FileDTO fileDTO = new FileDTO();
            List<String> images = new ArrayList<>();
            int count = new Random().nextInt(4);
            fileDTO.setType(count);
            fileDTO.setImages(images);
            if (count > 0){
                for (int i = 0; i < count; i++) {
                    String fileName = i % 4 + ".jpg";
                    images.add("http://localhost:9090/"+ fileName);
                }
            }
            dto.setFileDTO(fileDTO);
            return dto;
        }).collect(Collectors.toList());
        return new PagedReply<>(request.getIndex(), request.getPageSize(), pageInfoList.getTotal(), list2);
    }

    /**
     * 测试分页查询
     */
    @RequestMapping("/car/pageQuery2")
    @ResponseBody
    public PagedReply<CarInfoPageDTO> pageQuery2(@RequestParam("index")Integer index,@RequestParam("pageSize") Integer pageSize) throws InterruptedException {
        log.info("request===index:{},pageSize:{}",index,pageSize);
        Thread.sleep(1000);
        CarPageRequest request = new CarPageRequest();
        request.setIndex(index);
        request.setPageSize(pageSize);
        PageInfo<AdapterCarInfoEntity> pageInfoList = carInfoService.pageQuery(request);
        List<AdapterCarInfoEntity> list = pageInfoList.getList();
        List<CarInfoPageDTO> list2 = list.stream().map(t -> {
            CarInfoPageDTO dto = new CarInfoPageDTO();
            BeanUtils.copyProperties(t, dto);
            FileDTO fileDTO = new FileDTO();
            List<String> images = new ArrayList<>();
            int count = new Random().nextInt(4);
            if (count != 1 && count != 3 && count != 0){
                count = 3;
            }
            fileDTO.setType(count);
            fileDTO.setImages(images);
            if (count > 0){
                for (int i = 0; i < count; i++) {
                    String fileName = Math.abs(new Random().nextInt(1000) -1) % 12+ ".jpg";
                    images.add("http://localhost:9090/"+ fileName);
                }
            }
            dto.setFileDTO(fileDTO);
            dto.setCommCount(new Random().nextInt(200));
            return dto;
        }).collect(Collectors.toList());
        return new PagedReply<>(request.getIndex(), request.getPageSize(), pageInfoList.getTotal(), list2);
    }

}
